package com.kgis.fm.access.common.callback;

import java.util.ArrayList;
import java.util.List;

import com.kgis.kp.common.util.Detect;
import com.kgis.kp.model.Model;

/**
 * 
 * @author zhxiaomiao
 * 
 * 
 */
public abstract class GroupingAccessCallback<T extends Model> extends Detect {

	public abstract List<T> findGroup(long[] parameterGroup);

	public List<T> escapeDuplication(List<T> models) {
		return models;
	}

	public List<T> groupingFind(long[] parameters, int groupSize) {
		if (!notEmpty(parameters)) {
			return null;
		}
		/* escape duplicated value */
		long[] escapeDuplicationParameters = escapeDuplication(parameters);

		List<long[]> parameterGroups = grouping(escapeDuplicationParameters, groupSize);

		List<T> models = null;
		List<T> modelGroup = null;
		if (notEmpty(parameterGroups)) {
			for (long[] parameterGroup : parameterGroups) {
				modelGroup = findGroup(parameterGroup);
				if (null != modelGroup) {
					if (null == models) {
						models = new ArrayList<T>();
					}
					models.addAll(modelGroup);
				}
			}

			return escapeEmpty(escapeDuplication(models));
		}
		return null;
	}

}
